package state;

import java.util.Vector;

import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.SerialUtil;
import state.util.array.ByteArray;

public class ResourceRms {
  private static Hashtable recordStoreMap = new Hashtable();

  private static final String MASTER_SUFFIX = "master";
  private static final String RESOURCE_SUFFIX = "resource";

  public static Hashtable loadMasterRecord(String category) {
    Hashtable hashtable = new Hashtable();
    RecordStore masterRecordStore = null;
    masterRecordStore = RecordStore.openRecordStore(category + MASTER_SUFFIX);
    if ((masterRecordStore == null) || (masterRecordStore.getNumRecords() == 0)) {
      return null;
    }

    ByteArray data = masterRecordStore.getRecord(1);
    int index = 0;
    while (index < data.len() - 1) {
      int len = SerialUtil.getInt(data, index);
      index += 4;
      String key = SerialUtil.getString(data, index, len);
      index += len;
      int value = SerialUtil.getInt(data, index);
      index += 4;
      hashtable.put(key, new Integer(value));
    }
    return hashtable;
  }

  public static void saveMasterRecord(String category, Hashtable hashtable) {
    int len = 0;
    Vector keys = hashtable.keys();
    for (int i = 0; i < keys.size(); i++) {
      String key = String.valueOf(keys.elementAt(i));
      len += (4 + GameStringUtils.toByteArrayUTF8(key).len());
      len += 4; // 4 byte dành cho value
    }

    ByteArray data = ByteArray.create(len);
    keys = hashtable.keys();
    int index = 0;
    for (int i = 0; i < keys.size(); i++) {
      String key = String.valueOf(keys.elementAt(i));
      ByteArray bytes = GameStringUtils.toByteArrayUTF8(key);
      ByteArray.arraycopy(SerialUtil.serialNumberFromInt(bytes.len()), 0, data, index, 4);
      index += 4;
      ByteArray.arraycopy(bytes, 0, data, index, bytes.len());
      index += bytes.len();
      int value = ((Integer) hashtable.get(key)).intValue();
      ByteArray.arraycopy(SerialUtil.serialNumberFromInt(value), 0, data, index, 4);
      index += 4;
    }

    RecordStore.deleteRecordStore(category + MASTER_SUFFIX);
    RecordStore masterRecordStore = RecordStore.openRecordStore(category + MASTER_SUFFIX);
    masterRecordStore.addRecord(data, 0, data.len());
  }

  public static GameImage getImage(String category, int recordIndex) {
    RecordStore recordStore = getConnectionResource(category);
    if (recordStore.getNumRecords() < recordIndex) {
      return null;
    }
    ByteArray data = recordStore.getRecord(recordIndex);
    if (data == null) {
      return new GameImage(null, "", "");
    }
    return GameImage.createImage(data, 0, data.len());
  }

  public static ByteArray getResourceAsByteArray(String category, int recordIndex) {
    RecordStore recordStore = getConnectionResource(category);
    if (recordStore.getNumRecords() < recordIndex) {
      return null;
    }
    return recordStore.getRecord(recordIndex);
  }

  public static int saveImageData(String category, ByteArray data) {
    return getConnectionResource(category).addRecord(data, 0, data.len());
  }

  private static RecordStore getConnectionResource(String category) {
    RecordStore recordStore = (RecordStore) recordStoreMap.get(category);
    if (recordStore == null) {
      recordStore = RecordStore.openRecordStore(category + RESOURCE_SUFFIX);
      recordStoreMap.put(category, recordStore);
    }
    return recordStore;
  }
}
